(function ($app) {
    $app.module('gtPartyApp').controller('pf_djscgzListCtrl', function ($scope, $http, $modal, $ocLazyLoad, $alert, $loading, $state, $bsRouterState, AuthUser, $timeout, $excel, $partySelect, $filter) {
        $scope.loginUserId = AuthUser.getUser().Id;
        $scope.isShow = true;
        $scope.isAll = false;
        $scope.pageId = "pf_djscgzList";
        $scope.tabNameAdd = "home.pf_djscgzAdd";
        $scope.tabNameEdit = "home.pf_djscgzEdit";
        $scope.tabNameDetail = "home.pf_djscgzDetail";

        //定义数据集合
        $scope.dataList = []; //列表数据
        $scope.pxjbTypeList = [];
        $scope.pxlbTypeList = [];
        //组织树参数
        $scope.treeparams = {
            isunfold: false,//组织结构树展开收缩
            selectdzzdm: "",
            selectdzzmc: "",
            defaultselected: ''
        };
        $scope.resetTreeparams = angular.copy($scope.treeparams);

        //查询条件
        $scope.selectparams = {
            pageindex: 1,
            pagesize: 10,
            dzzdm: '',
            pxqdsjks: null,
            pxqdsjjs: null,
            pxjb: '',
            pxjgqdsjks: null,
            pxjgqdsjjs: null,
            pxlb: ''
        };
        $scope.resetSelectparams = angular.copy($scope.selectparams);
        $scope.pageInfo = {ptotal: 0};

        $scope.$on('tabChildReloadData', function (event, data) {
            if (data.name == 'pf_djscgzListCtrl') {
                $scope.loadData();
            }
        });

        $scope.loadPxjbTypes=function(){

            $http({
                method: 'get',
                url: '../../api/dictionary/getDictionaryListByDicTypeKey',
                params: {dicTypeKey: 'pxjbType'}
            }).then(function (result) {
                $scope.pxjbTypeList = result.data;
            }, function (resp) {

            });
        };
        $scope.loadPxjbTypes();

        $scope.loadPxlbTypes=function(){

            $http({
                method: 'get',
                url: '../../api/dictionary/getDictionaryListByDicTypeKey',
                params: {dicTypeKey: 'pxlbType'}
            }).then(function (result) {
                $scope.pxlbTypeList = result.data;
            }, function (resp) {

            });
        };
        $scope.loadPxlbTypes();

        //加载列表数据
        $scope.loadData = function () {
            $http
            ({
                method: 'get', url: '../../api/partylife/djscgz/getList', params: $scope.selectparams
            }).then(function (result) {
                $scope.dataList = result.data.item.list;
                $scope.pageInfo.ptotal = result.data.item.total;
            }, function (res) {
                $scope.showMsg('错误', '服务器错误');
            });
        };

        $scope.search = function () {
            $scope.loadData();
        };

        $scope.selectAll = function () {
            $scope.isAll = !$scope.isAll;
            angular.forEach($scope.dataList, function (item) {
                item.checked = $scope.isAll ? 1 : 0;
            });
        };

        //查看
        $scope.show = function (id) {
            $bsRouterState.$closeTabName($scope.tabNameDetail);
            $timeout(function () {
                $bsRouterState.go($scope.tabNameDetail, {
                    id: id
                });
            }, 100);
        };

        //新增
        $scope.add = function () {
            $bsRouterState.$closeTabName($scope.tabNameAdd);
            $timeout(function () {
                $bsRouterState.go($scope.tabNameAdd, {
                    id: ''
                });
            }, 100);
        };

        //修改
        $scope.edit = function (id) {
            $bsRouterState.$closeTabName($scope.tabNameEdit);
            $timeout(function () {
                $bsRouterState.go($scope.tabNameEdit, {
                    id: id
                });
            }, 100);
        };

        $scope.export = function () {
            var params = "?1=1";
            for (var p in $scope.selectparams) {
                if ($scope.selectparams[p] !== '' && $scope.selectparams[p] !== null)
                    params += ("&" + p + "=" + encodeURI($scope.selectparams[p]));
            }
            window.open('../../api/partylife/djscgz/export' + params);
        };

        $scope.delete = function (id) {
            var ids = [];
            if (id)
                ids.push(id);
            else
                ids = $scope.dataList.filter(it => it.checked == true).map(it => it.id);

            if (ids.length == 0) {
                $scope.showMsg('消息', "请选择需要删除的数据");
                return false;
            }

            if (confirm("确定删除？")) {
                $loading.show();
                $http({
                    method: "post",
                    url: "../../api/partylife/djscgz/delete",
                    data: {
                        ids: JSON.stringify(ids)
                    }
                }).then(function (result) {
                    $loading.hide();
                    if (result.data.success) {
                        $scope.showMsg('成功', result.data.msg);
                        //重新刷新列表
                        $scope.loadData();
                    } else {
                        $scope.showMsg('失败', result.data.msg);
                    }
                }, function (resp) {
                    $scope.showMsg('错误', '服务器错误');
                });
            }
        };

        $scope.$watch("selectparams.pageindex", function (newVal, oldVal) {
            if ($scope.pageInfo.ptotal > 0) {
                $scope.loadData();
            }
        });
        $scope.$watch("treeparams.selectdzzdm", function (newVal, oldVal) {
            if (newVal != "") {
                if ($scope.treeparams.defaultselected != "" && $scope.treeparams.defaultselected != null &&
                    $scope.treeparams.defaultselected != $scope.treeparams.selectdzzdm)
                    return;

                $scope.selectparams.dzzdm = $scope.treeparams.selectdzzdm;

                $scope.loadData();
            }
        });
        $scope.setisunfold = function () {
            $scope.treeparams.isunfold = !$scope.treeparams.isunfold;
            $scope.$emit('menudatas.toggle', !$scope.treeparams.isunfold);
        };
        $scope.reset = function () {
            $scope.selectparams = angular.copy($scope.resetSelectparams);
            $scope.treeparams = angular.copy($scope.resetTreeparams);
            $scope.loadData();
        };

        $scope.loadData();
    });
})(angular);